package com.rjt.cucc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.common.util.CookieUtil;
import com.rjt.common.util.MD5Util;
import com.rjt.common.util.TextUtil;

import blade.kit.http.HttpRequest;

/**  

* <p>Title: AppCtroller.java</p>  

* <p>Description: </p>  

* @author renjiangtao 

* @date 2018年6月25日  

* @version 1.0  

*/
@Controller
@RequestMapping("flow")
public class FlowController {
	
	private Logger logger = LoggerFactory.getLogger(FlowController.class);
	
	private  String cookie;
	
	@GetMapping(value = "/index")
	public String testPage(HttpServletRequest request, HttpServletResponse response) {
		logger.warn("ip:{} 500M流量活动",TextUtil.getRemortIP(request));
		return "flow";
	}
	
	@PostMapping("login")
	@ResponseBody
	public String radomLogin(String mobile,String password) {
		try {
			String url="http://www.szshopping.cn/LoginUser2";
	        HttpRequest request = HttpRequest.post(url,true,
	        		"password",MD5Util.getMD5(password),
	        		"systemVersion","5.1.1",
	        		"networkType","WIFI",
	        		"operator","中国移动",
	        		"appVersion","4.0.3",
	        		"systemName","android",
	        		"MAC","",
	        		"phoneName","SM-G9350",
	        		"phone",mobile,
	        		"phoneID","865166024645593"
	        		);
	        String res = request.body();
	        this.cookie = CookieUtil.getCookie(request);
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@GetMapping("JoinOutActivity")
	@ResponseBody
	public  String joinOutActivity(String UID) {
		try {
			String url="http://www.szshopping.cn/JoinOutActivity?UID="+UID;
	        HttpRequest request = HttpRequest.get(url,true).header("Cookie",this.cookie);
	        String res = request.body();
			request.disconnect();
			if(res.indexOf("success")!=-1) {
				logger.error("mobile:{} 500m流量活动,mobile");
			}	
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	@GetMapping("GetOutActivityMessage")
	@ResponseBody
	public  String getOutActivityMessage(String UID) {
		try {
			String url="http://www.szshopping.cn/GetOutActivityMessage?AID=5aefed39e52d485548aee877&UID="+UID;
	        HttpRequest request = HttpRequest.get(url,true).header("Cookie",this.cookie);
	        String res = request.body();
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@PostMapping("GetRegisterCode")
	@ResponseBody
	public String getRegisterCode(String mobile) {
		try {
			String url="http://www.szshopping.cn/GetRegisterCode";
	        HttpRequest request = HttpRequest.post(url,true,
	        		"phone",mobile
	        		);
	        String res = request.body();
	        this.cookie = CookieUtil.getCookie(request);
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@PostMapping("RegisterUser")
	@ResponseBody
	public String registerUser(String mobile,String password,String code) {
		try {
			String url="http://www.szshopping.cn/RegisterUser";
	        HttpRequest request = HttpRequest.post(url,true,
	        		"phone",mobile,
	        		"password",password,
	        		"code",code
	        		);
	        String res = request.body();
	        this.cookie = CookieUtil.getCookie(request);
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
}
